<?php
    require_once "config.php";
    require_once(dirname(__FILE__).'/include/config.inc.php');
    require_once(dirname(__FILE__).'/include/mysqli.class.php');
    $id = $_POST['id'];
    $order_state = $mysql->query("UPDATE dyspw_goods_order SET state = 2  where id={$id}");
    $goods_order = $mysql->query("select * from dyspw_goods_order where id = {$id}");
    $goodsorder = $goods_order->fetch_all(MYSQLI_ASSOC);
    if($order_state){
        if($goodsorder[0]['order_type'] == 'waimai'){
			require_once "shipping.php";
			$Shipping = new Shipping();
			$waimai_sql = $mysql->query("select * from dyspw_shop_waimai where uid = {$goodsorder[0]['shop_id']}");
			$waimai = $waimai_sql->fetch_all(MYSQLI_ASSOC);
			$address_sql = $mysql->query("select * from dyspw_mem_address where id = {$goodsorder[0]['add_id']}");
			$address = $address_sql->fetch_all(MYSQLI_ASSOC);
			$params = array(
				'dispatch_mode' 	=> 1,
				'logistic' 	=> 'shansong',
				'shop_id' 	=> $waimai[0]['id'],
				'origin_id' 	=> $goodsorder[0]['ordernum'],
				'is_subscribe' 	=> 0,
				'subscribe_time' 	=> strtotime('+1hour'),
				'receiver_longitude' 	=> $address[0]['add_lng'],
				'receiver_latitude' 	=> $address[0]['add_lat'],
				'receiver_address' 	=> $address[0]['add_place'],
				'receiver_address_detail' 	=> $address[0]['add_brand'],
				'receiver_name' 	=> $address[0]['add_link'],
				'receiver_phone' 	=> $address[0]['add_mobile'],
				'map_type' 	=> 1,
				'callback_url' 	=> 'https://5p8.fyh5p8.com/xiaochengxu/found_order.php',
			);
			$shop_add = $Shipping->GetMaiYaTianData('/order/add/',$params);
//			print_r($shop_add);exit;
			if($shop_add['message'] == 'success'){
                $match_info = $Shipping->GetMaiYaTianData('/order/detail/',array('order_id'=>$shop_add['data']['order_id']));
				$mysql->query("UPDATE dyspw_goods_order SET  match_id='".$shop_add['data']['order_id']."' , match_state = {$match_info['data']['order_status']}  where ordernum=".$shop_add['data']['origin_id']);

                $goodsInfo=$goodsorder[0];
                $printInfo = $dosql->GetOne("select a.*,b.username from sys_printer as a left join dyspw_member as b on a.uid = b.id where a.uid = ".$goodsInfo['shop_id']);
//                $printInfo = $dosql->GetOne("select a.username,b.* from dyspw_member as a left join sys_printer as b on a.id = b.uid where a.id = ".$goodsInfo['shop_id']);
                if($printInfo){
                    $goodsData=json_decode($goodsInfo['order_details'],true);
                    if($goodsData){
                        //商户绑定打印机
                        require_once(dirname(__FILE__).'/include/common.inc.php');
                        //配送地址信息
                        $address = $dosql->GetOne("select * from dyspw_mem_address where id=".$goodsInfo['add_id']);
                        //商品信息
                        $pirnt=new printTicket();
                        $content = '<CB>'.$printInfo['username'].'</CB><BR>';
                        $content .= '<CB>--已在线支付--</CB><BR>';
                        $content .= '<B>期望送达时间：立即送餐</B><BR><BR>';
                        $content .= '名称　　　　　 单价  数量 金额<BR>';
                        $content .= '--------------------------------<BR>';
                        foreach ($goodsData as $val){
                            $content .= $title.str_repeat(' ',(16 - 2*mb_strlen($val['title']))).$val['money'].$val['num'].'  '.$val['money']*$val['num'].'<BR>';
                        }
                        $content .= '--------------------------------<BR>';
                        $content .= '备注：'.$goodsInfo['remarks'].'<BR>';
                        $content .= '包装费：'.$goodsInfo['pack_fee'].'元<BR>';
                        $content .= '配送费：'.$goodsInfo['delivery_fee'].'元<BR>';
                        $content .= '总计：'.$goodsInfo['Totalprice'].'元<BR>';
                        $content .= '送货地点：'.$address['add_place'].'<BR>';
                        $content .= '联系人：'.$address['add_link'].'<BR>';
                        $content .= '联系电话：'.$address['add_mobile'].'<BR>';
                        $content .= '订餐时间：'.date('Y-m-d H:i:s',$goodsInfo['time']).'<BR>';
                        $content .= '订单编号：'.$goodsInfo['ordernum'].'<BR>';
                        if($printInfo['print_type']) {
                            //xp打印
                            $content = '<BL><CB> --在线支付--<BR>
                                <HB> ' . $printInfo['username'] . '<BR>
                                <BL><L><N>' . date('Y-m-d H:i:s', $goodsInfo['time']) . '<BR>
                                订单编号：' . $goodsInfo['ordernum'] . '<BR>
                                ***************商品*************<BR>';
                            foreach ($goodsData as $val) {
                                $content .= '<BL><L><HB>' . $val['title'] . str_repeat(' ', (16 - 2 * mb_strlen($val['title']))) . ' x' . $goodsInfo['num'] . ' ' . $goodsInfo['money'] * $goodsInfo['num'] . '<BR>';
                            }
                            $content .= '<N>................................<BR>
                                配送费：￥' . $goodsInfo['delivery_fee'] . '<BR>
                                餐盒费：￥' . $goodsInfo['pack_fee'] . '<BR>
                                ********************************<BR>
                                总计：￥' . $goodsInfo['Totalprice'] . '<BR>
                                <HB>送货地址' . $address['add_place'] . '<BR>
                                <HB>联系人' . $address['add_link'] . '<BR>
                                <HB>联系电话' . $address['add_mobile'] . '<BR>
                                <BL>';
                        }
                    }
                    $pirnt->printOrder($content,$printInfo['print_sn'],$printInfo['print_type']);
//                    $result=$pirnt->printOrder($content,'918541090',$printInfo['print_type']);
                }
			}
            $mysql->query("UPDATE dyspw_goods_order SET state = 1  where id={$id}");
            $result['status'] = 'y';
            $result['info'] = $shop_add['message'];
            exit(json_encode($result));
		}
        $result['status'] = 'y';
        $result['info'] = '接单成功';
        exit(json_encode($result));
    }else{
        $result['status'] = 'n';
        $result['info'] = '接单失败，请重新提交';
        exit(json_encode($result));
    }
 ?>
